MOMENT

Overview

The MOMENT function calculates the nth central moment about the mean for a sample of data. In statistics, a moment is a quantitative measure that describes the shape of a distribution. Central moments, which measure deviations from the mean, are fundamental building blocks for calculating other important statistics like variance, skewness, and kurtosis.

This implementation uses the scipy.stats.moment function from SciPy, a comprehensive scientific computing library for Python. For detailed API information, see the SciPy moment documentation.

The kth central moment of a sample is computed as:

m_k = \frac{1}{n} \sum_{i=1}^{n} (x_i - \bar{x})^k

where n is the number of observations, x_i represents each data point, and \bar{x} is the sample mean. The function uses the number of observations as the denominator without degrees of freedom correction.

Different moment orders reveal different characteristics of the distribution:

  • 1st moment: Always zero when calculated about the mean
  • 2nd moment: The variance (spread of data around the mean)
  • 3rd moment: Related to skewness (asymmetry of the distribution)
  • 4th moment: Related to kurtosis (heaviness of the tails)

Higher-order moments provide increasingly detailed information about tail behavior and distribution shape, though they require larger sample sizes to estimate reliably. The function handles non-numeric values by ignoring them and requires at least two numeric values to compute a result.

This example function is provided as-is without any representation of accuracy.

Excel Usage

=MOMENT(data, order)
  • data (list[list], required): 2D array of numeric values. Non-numeric values are ignored.
  • order (int, required): Order of the moment to calculate (must be a positive integer).

Returns (float): The nth moment about the mean, or error message (str) if input is invalid.

Examples

Example 1: Second moment (variance) of a column vector

Inputs:

data order
1 2
2
3
4
5

Excel formula:

=MOMENT({1;2;3;4;5}, 2)

Expected output:

2

Example 2: Third moment (skewness) of a row vector

Inputs:

data order
1 2 3 4 5 3

Excel formula:

=MOMENT({1,2,3,4,5}, 3)

Expected output:

0

Example 3: Fourth moment (kurtosis) of a matrix

Inputs:

data order
1 2 3 4
4 5 6

Excel formula:

=MOMENT({1,2,3;4,5,6}, 4)

Expected output:

14.7299

Example 4: First moment of a column vector (always zero)

Inputs:

data order
10 1
20
30

Excel formula:

=MOMENT({10;20;30}, 1)

Expected output:

0

Python Code

from scipy.stats import moment as scipy_moment
import math

def moment(data, order):
    """
    Calculates the nth moment about the mean for a sample.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.moment.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        data (list[list]): 2D array of numeric values. Non-numeric values are ignored.
        order (int): Order of the moment to calculate (must be a positive integer).

    Returns:
        float: The nth moment about the mean, or error message (str) if input is invalid.
    """
    def to2d(x):
        return [[x]] if not isinstance(x, list) else x

    data = to2d(data)

    if not isinstance(data, list) or not all(isinstance(row, list) for row in data):
        return "Invalid input: data must be a 2D list."

    flat = []
    for row in data:
        for x in row:
            try:
                val = float(x)
                if math.isfinite(val):
                    flat.append(val)
            except (TypeError, ValueError):
                continue

    if len(flat) < 2:
        return "Invalid input: data must contain at least two numeric values."

    if not isinstance(order, (int, float)) or int(order) != order or order < 1:
        return "Invalid input: order must be a positive integer."

    result = scipy_moment(flat, moment=int(order), axis=None, nan_policy='omit')
    return float(result)

Online Calculator